package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.MarketRole;
import com.cskaoyan.bean.bo.role.PermissionsBo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.admin.role.MarketModuleLevelBVo;

import com.cskaoyan.service.admin.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zjq
 * 角色管理
 * url-->/admin/role/*
 * @date 2022/11/19 17:34
 */
@RestController
@RequestMapping("/admin/role")
public class RoleController {

    @Autowired
    RoleService roleService;

    @RequestMapping("options")
    public BaseRespVo options(){
        CommonData query = roleService.query(null, null, null, null);
        return BaseRespVo.ok(query);
    }

    @RequestMapping("/list")
    public BaseRespVo list(Integer id,String name,Integer page,Integer limit){

        CommonData queryList=roleService.queryList(id, name, page, limit);
        return BaseRespVo.ok(queryList);
    }

    @RequestMapping("/create")
    public BaseRespVo create(@RequestBody MarketRole role){
        MarketRole marketRole = roleService.create(role);
        if (marketRole == null) {
            return BaseRespVo.error(-1,"名称已存在");
        }
        return BaseRespVo.ok(marketRole);
    }

    @RequestMapping("/update")
    public BaseRespVo update(@RequestBody MarketRole role) {

        int update = roleService.update(role);

        if (update > 0) {
            return BaseRespVo.ok(role);
        }
        else {
            return BaseRespVo.error(-1,"名称已存在");
        }
    }
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody MarketRole role) {
        int delete = roleService.delete(role);
        if(delete > 0){
            return BaseRespVo.ok();
        }
        return BaseRespVo.error(-1,"删除失败");
    }

    /**
     * @author zjq
     * @date 2022/11/22 13:45
     */
    @GetMapping("permissions")
    public BaseRespVo permissions(Integer roleId){
        MarketModuleLevelBVo.PermissionsVo permissionsVo = roleService.queryModulePermissions(roleId);

        return BaseRespVo.ok(permissionsVo);
    }
    /**
     * @author zjq
     * @date 2022/11/22 15:47
     */
    @PostMapping("permissions")
    public BaseRespVo update(@RequestBody PermissionsBo perm){
        Integer roleId = perm.getRoleId();
        List<String> permissions = perm.getPermissions();
        roleService.updatePermissions(roleId,permissions);
        return BaseRespVo.ok();
    }

}
